layui.config({
    base: '/home/js/util/'
}).use(['element','laypage','form','menu','jquery'],function(){
    element = layui.element,laypage = layui.laypage,form = layui.form,menu = layui.menu,$ = layui.jquery;

    function getQueryString(name) {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        };
        return null;
    };


    var rendDetails = function (value) {

        //获取本地新闻缓存 ，用于显示上一条下一条
        let newsList = JSON.parse(window.sessionStorage.getItem("news_list"));
        let number = -1;

        var html = '<div class="article-cont">\n' +
            '                  <div class="title">\n' +
            '                    <h1>'+value.newsTitle +'</h1>\n' +
            '                    <p class="cont-info"><span class="data">'+value.newsAnnounceTime+'</span><span class="types">'+value.newsType +'</span></p>\n' +
            '                  </div>\n' +
            '                  <div class="newsbody">\n' +
            value.newsContent+'<p class="types">本文来源:'+value.newsMedia+'</p>'+
            '<p class="types">>>>>>>>>>>>>>>> <a target="_blank" href="'+value.newsFromUrl+'">查看原文</a></p>\n' +
            '                  </div>\n' +
            '                  <div class="btn-box">\n' +
            '                    <button id="pre" class="layui-btn layui-btn-primary">上一篇</button>\n' +
            '                    <button id="next" class="layui-btn layui-btn-primary">下一篇</button>\n' +
            '                  </div>\n' +
            '                </div>';
        $("#article-context").html(html);

        $('bady').on('click','#pre',function () {
            $.each(newsList,function (index,value) {
                if (value.newsId == getQueryString("news")){
                    number = index;
                }
            });
           location.href="/home/details.html?news="+newsList[number-1 % newsList.length].newsId;
        });

        $('body').on('click','#next',function () {
            $.each(newsList,function (index,value) {
                if (value.newsId == getQueryString("news")){
                    number = index;
                }
            });
            location.href="/home/details.html?news="+newsList[number+1 % newsList.length].newsId;
        });

    };




    var loadNews = function (){
        var newsId = getQueryString("news");
        CoreUtil.sendAjax("/news/byId/"+newsId,'' , function (res) {
            if (res.data != null) {
                rendDetails(res.data);
            } else {
                layer.msg('查询失败', {icon: 2});
            }
        }, "POST", false, function (res) {
            layer.msg('站点出问题啦！请联系管理员', {icon: 6})
        });
    };







    //监听提交评论
    form.on('submit(submit-desc)',function (date){
       let desc = date.field.desc;

       if (desc == null || desc == '' || $.trim(desc) == ''){
           layer.msg("请填写评论信息！",{icon:2});
           return false;
       }

        let param = {
            desc : desc
        };

        $.ajax({
            url:'/comment/addComment/'+getQueryString("news"),
            type:'POST', //GET
            async:true,    //或false,是否异步
            data: JSON.stringify(param),
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            contentType: "application/json",
            success:function(data){
               if (data.code == '0'){
                   layer.msg("评论成功！",{icon:1});
                   loadComment();
               }else {
                   layer.msg(data.msg,{icon:2});
               }
            }
        });
        return false;
    });

    var param = {
        req: {
        },
        page: 1,
        limit: 10,
    };

    var  renderData = function (data){

        var html = '';

        $.each(data,function (index,value){

            html += '<div class="cont">\n' +
                '                  <div class="img">\n' +
                '                    <img src="/home/img/header.png" alt="">\n' +
                '                  </div>\n' +
                '                  <div class="text">\n' +
                '                    <p class="tit"><span class="name">游客</span><span class="data">'+ value.createtime +'</span></p>\n' +
                '                    <p class="ct">'+value.commentText +'</p>\n' +
                '                  </div>\n' +
                '                </div>';
        });

        $('#comment-list').html(html);
        form.render();
    };

    var loadComment = function(){

        $.ajax({
            url:'/comment/getComment4NewsId/'+getQueryString("news"),
            type:'POST', //GET
            async:true,    //或false,是否异步
            data: JSON.stringify(param),
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            contentType: "application/json",
            success:function(data) {

                if (data.code == '0'){
                    $('#comment-total').html(data.data.total);
                    /*评论分页条*/
                    if (data.data.total <= 1){
                        return;
                    }
                    laypage.render({
                        elem: 'comment-page'
                        ,count: data.data.total //数据总数，从服务端得到
                        ,limit: param.limit,
                        curr: param.page,
                        layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                        jump: function (obj, first) {
                            if (!first) {
                                param.page = obj.curr;
                                param.limit = obj.limit;
                                loadComment();
                            }
                        }
                    });

                    renderData(data.data.records);
                }
            }
        });

    };

    loadNews();
    menu.init();
    menu.submit();
    loadComment();
})